﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AD.Model;
using AD.BLL;
using System.Web.Caching;

/// <summary>
///DataCache 的摘要说明
/// </summary>
public class HttpDataCache
{
    private static IDictionary<string, PayTradeMod> pays;

    public static List<PresentChargeMod> QueryList()
    {
        string key = "PresentCharges";
        List<PresentChargeMod> pesentCharges = HttpRuntime.Cache[key] as List<PresentChargeMod>;
        if (pesentCharges == null)
        {
            pesentCharges = PresentChargeBLL.QueryFilterToList("");
            if (pesentCharges != null)
                HttpRuntime.Cache.Add(key, pesentCharges, null, DateTime.Now.AddSeconds((double)UtilHepler.AppDataTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
        }
        return pesentCharges;
    }

    public static SystemSettingMod AppSettings
    {
        get
        {
            string key = "system_setting";
            SystemSettingMod pesentCharges = HttpRuntime.Cache[key] as SystemSettingMod;
            if (pesentCharges == null)
            {
                pesentCharges = SystemSettingBLL.QueryFirst();
                if (pesentCharges != null)
                    HttpRuntime.Cache.Add(key, pesentCharges, null, DateTime.Now.AddSeconds((double)UtilHepler.AppDataTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return pesentCharges;
        }
    }
    public static void RemoveAppSettings()
    {
        try
        {
            string key = "system_setting";
            HttpRuntime.Cache.Remove(key);
        }
        catch { }
     }
    
    public static List<HeplerMod> Heplers
    {
        get
        {
            string key = "hepler_setting";
            List<HeplerMod> pesentCharges = HttpRuntime.Cache[key] as List<HeplerMod>;
            if (pesentCharges == null)
            {
                pesentCharges = HeplerBLL.QueryFilterToList("");
                if (pesentCharges != null)
                    HttpRuntime.Cache.Add(key, pesentCharges, null, DateTime.Now.AddSeconds((double)UtilHepler.AppDataTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return pesentCharges;
        }
    }

    public static List<NoticeMod> Notices
    {
        get
        {
            string key = "notice_setting";
            List<NoticeMod> pesentCharges = HttpRuntime.Cache[key] as List<NoticeMod>;
            if (pesentCharges == null)
            {
                pesentCharges = NoticeBLL.QueryFilterToList("");
                if (pesentCharges != null)
                    HttpRuntime.Cache.Add(key, pesentCharges, null, DateTime.Now.AddSeconds((double)UtilHepler.AppDataTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return pesentCharges;
        }
    }

    public static void Init()
    {
        try
        {
            SystemSettingMod d = AppSettings;
            List<HeplerMod> d1 = Heplers;
            List<NoticeMod> d2 = Notices;
            QueryList();
        }
        catch { }
    }

    static HttpDataCache()
    {
        pays = new Dictionary<string, PayTradeMod>();
        //SystemSetting = SystemSettingBLL.QueryFirst();
    }

    public static PayTradeMod GetPay(string payTradeNo)
    {
        if (pays.ContainsKey(payTradeNo))
            return pays[payTradeNo];
        return null;
    }

    public static bool RemovePay(string payTradeNo)
    {
        if (pays.ContainsKey(payTradeNo))
            return pays.Remove(payTradeNo);
        return false;
    }

    public static bool AddPay(PayTradeMod payTrade)
    {
        if (payTrade != null && pays.ContainsKey(payTrade.Out_Trade_No))
        {
            pays.Add(payTrade.Out_Trade_No, payTrade);
            return true;
        }
        return false;
    }

}